/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */

package l1j.server.server.serverpackets;

import java.util.Iterator;
import java.util.List;

import javolution.util.FastTable;
import l1j.opqlo.Server.L1World.L1World;
import l1j.opqlo.假人系統.A_DummyTable;
import l1j.opqlo.假人系統.L1Dummy;
import l1j.server.server.datatables.ItemTable;
import l1j.server.server.model.L1Object;
import l1j.server.server.model.Instance.L1DummyInstance;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.templates.L1Item;
import l1j.server.server.templates.L1PrivateShopBuyList;
import l1j.server.server.templates.L1PrivateShopSellList;

// Referenced classes of package l1j.server.server.serverpackets:
// ServerBasePacket

public class S_PrivateShop extends ServerBasePacket {

	public S_PrivateShop(final L1PcInstance pc, final int objectId,final int type) {
		final L1Object obj =  L1World.getInstance().findObject(objectId);
		L1PcInstance shopPc = null;
		if(obj instanceof L1PcInstance){
			shopPc = (L1PcInstance) obj;
		}
		if (shopPc == null) {
			return;
		}

		if (type == 0) {

			this.writeC(S_OPCODE_SHOWSHOPBUYLIST);
			// writeC(type);
			this.writeD(objectId);

			final List<L1PrivateShopSellList> list = shopPc.getSellList();
			final int size = list.size();
			pc.setPartnersPrivateShopItemCount(size);
			this.writeH(size);
			final L1ItemInstance dummy = new L1ItemInstance();

			for (int i = 0; i < size; i++) {
				final L1PrivateShopSellList pssl = list.get(i);
				final int itemObjectId = pssl.getItemObjectId();
				final int count = pssl.getSellTotalCount()
						- pssl.getSellCount();
				// final int count =pssl.getSellCount();
				final int price = pssl.getSellPrice();
				final L1ItemInstance item = shopPc.getInventory().getItem(itemObjectId);
				if (item != null) {
					this.writeD(i);
					this.writeH(item.getItem().getGfxId());
					this.writeD(price);
					if (count > 1) {
						this.writeS(item.getName() + " (" + count + ")");
					} else {
						if (item.getRemainingTime() > 0) {
							this.writeS(item.getViewName() + "(限時:"+ item.getRemainingTime() + "秒)");
						} else {
							this.writeS(item.getViewName());
						}
					}
					final L1Item template = ItemTable.getInstance()
							.getTemplate(item.getItemId());
					if (template == null) {
						this.writeC(0);
					} else {
						dummy.setItem(template);
						final byte[] status = dummy.getStatusBytes();
						this.writeC(status.length);
						for (final byte b : status) {
							this.writeC(b);
						}
					}
				}
			}
			this.writeH(0x07); // 0x00:kaimo 0x01:pearl 0x07:adena
			/*
			 * writeC(Opcodes.S_OPCODE_PRIVATESHOPLIST); writeC(type);
			 * writeD(objectId); final List<L1PrivateShopSellList> list =
			 * shopPc.getSellList(); final int size = list.size();
			 * pc.setPartnersPrivateShopItemCount(size); writeH(size);
			 * 
			 * for (int i = 0; i < size; i++) { final L1PrivateShopSellList pssl
			 * = (L1PrivateShopSellList) list.get(i); final int itemObjectId =
			 * pssl.getItemObjectId(); final int count =
			 * pssl.getSellTotalCount() - pssl.getSellCount(); final int price =
			 * pssl.getSellPrice(); final L1ItemInstance item =
			 * shopPc.getInventory().getItem(itemObjectId);
			 * 
			 * if (item != null) { writeC(i); writeC(item.getBless());
			 * writeH(item.getItem().getGfxId()); writeD(count); writeD(price);
			 * writeS(item.getNumberedViewName(count)); writeC(0);
			 * 
			 * } }
			 */

		} else if (type == 1) {

			this.writeC(S_OPCODE_PRIVATESHOPLIST);
			this.writeC(type);
			this.writeD(objectId);

			final List<L1PrivateShopBuyList> list = shopPc.getBuyList();
			Iterator<L1PrivateShopBuyList> iterator =list.iterator();
			this.writeH(list.size());
			//			for (int i = 0; i < size; i++) {
			int i=0;
			while(iterator.hasNext()){
				//				final L1PrivateShopBuyList psbl = list.get(i);
				final L1PrivateShopBuyList psbl = iterator.next();
				final int itemObjectId = psbl.getItemObjectId();
				final int count = psbl.getBuyTotalCount();
				final int price = psbl.getBuyPrice();
				final L1ItemInstance item = shopPc.getInventory().getItem(itemObjectId);
				if(item == null){
					iterator.remove();
					System.out.println("NULL");
				}else{
					for (final L1ItemInstance pcItem : pc.getInventory().getItems()) {
						if (item.getItemId() == pcItem.getItemId()
								&& item.getEnchantLevel() == pcItem.getEnchantLevel()) {
							this.writeC(i);
							this.writeD(pcItem.getId());
							this.writeD(count);
							this.writeD(price);
						}
					}
				}
				i++;
			}
		}
	}
	public S_PrivateShop(L1DummyInstance npc)
	{
		FastTable<L1Dummy> shop = A_DummyTable.get().getSellList().get(npc.getNpcId());

		if (shop == null)
		{
			System.out.println("[NPC識別碼:" + npc.getNpcId() + "] 沒有任何的資料，在此不做任何的動作！");
			return;
		}

		writeC(S_OPCODE_PRIVATESHOPLIST);
		writeC(0x00); // 個人商店
		writeD(npc.getId());
		writeH(shop.size());

		for (int i = 0; i < shop.size(); i++)
		{
			L1Dummy ns = shop.get(i);

			writeC(i);
			writeC(ns.getItem().getItem().getBless());
			writeH(ns.getItem().get_gfxid());
			writeD((int) ns.getCount());
			writeD((int) ns.getMoney());
			final long count = ns.getCount();
			L1ItemInstance item = ns.getItem();
			if (count > 1) {
				this.writeS(item.getName() + " (" + count + ")");
			} else {
				if (item.getRemainingTime() > 0) {
					this.writeS(item.getViewName() + "(限時:"+ item.getRemainingTime() + "秒)");
				} else {
					this.writeS(item.getViewName());
				}
			}
			writeC(0x00);
		}
	}


	@Override
	public byte[] getContent() {
		return this.getBytes();
	}
}
